package org.ccay.web.support.internal.impl;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.ccay.core.request.Application;
import org.ccay.core.request.IRequestContext;
import org.ccay.core.request.impl.RequestContext;
import org.ccay.core.request.impl.RequestContextConstants;
import org.ccay.core.util.StringUtil;

/**
 *  RequestContext 初始化辅助工具	
 */
public final class RequestContextInitUtil {
	 
	
	private RequestContextInitUtil(){
	}
	/**
	 * 构建基本的请求上下文，但不包含用户信息的构建
	 * @param request
	 * @return
	 */
	public static IRequestContext buildBasicContext(HttpServletRequest request) {		
		RequestContext requestContext = new RequestContext();		
		
		processSession(request, requestContext); 		
		processApplication(requestContext);
		processLanguage(request, requestContext);
		processIp(request, requestContext);
		processOthers(request, requestContext);
		
		return requestContext;
	}

	/**  
	 * @param request
	 * @param requestContext
	 */
	private static void processOthers(HttpServletRequest request, RequestContext requestContext) {
		//初始化请求URI
		requestContext.setItem(RequestContextConstants.KEY_URI,   RequestUtil.getShortUri(request));
		//初始化请求类型
		requestContext.setItem(RequestContextConstants.KEY_REQUEST_TYPE, request.getAttribute(RequestContextConstants.KEY_REQUEST_TYPE));
		//wx244051 增加初始化cookie信息
		requestContext.setItem(RequestContextConstants.KEY_COOKIES_TYPE, RequestUtil.getCookies(request));
		//获取请求头中的特定属性
		String headerStr = request.getHeader("systemHeaderConstants");
		try {
			if(!StringUtil.isNullOrEmpty(headerStr)){
				requestContext.setItem(RequestContextConstants.KEY_HEADER, URLDecoder.decode(headerStr, "UTF-8"));
			}
		} catch (UnsupportedEncodingException e) {
			requestContext.setItem(RequestContextConstants.KEY_HEADER, headerStr);
		}
		//TODO 内部识别TOKEN
//		String internalToken = request.getHeader(SecurityConstants.INTERNAL_TOKEN);
//		if(!StringUtil.isNullOrEmpty(internalToken)){
//			requestContext.setItem(SecurityConstants.INTERNAL_TOKEN, internalToken);
//		}
	}

	/**  
	 * @param request
	 * @param requestContext
	 */
	private static void processIp(HttpServletRequest request, RequestContext requestContext) {
		// 获取用户IP
		String userIp = (request).getRemoteAddr();
		requestContext.setUserIp(userIp);
	}

	/**  
	 * @param request
	 * @param requestContext
	 */
	private static void processLanguage(HttpServletRequest request, RequestContext requestContext) {
		// 设置用户语言
		String userLanguage = RequestLangUtil.getUserLang(request);
		requestContext.setUserLanguage(userLanguage);
	}

	/** 
	 * @param requestContext
	 */
	private static void processApplication(RequestContext requestContext) {		
		requestContext.setApplication(Application.getCurrent());
	}

	/**  
	 * @param request
	 * @param requestContext
	 */
	public static void processSession(HttpServletRequest request, RequestContext requestContext) {
		//Use Shior Session
		Session session = SecurityUtils.getSubject().getSession(false);
		if(session == null){
			return;
		}
		requestContext.setSessionId(session.getId().toString());
	}
}
